package np13.sddb.queries;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import np13.Slime;
import np13.Smell;
import np13.sddb.Result;

/**
 * Represents an add {@link Query}. An add query is used to add a smell to a
 * record in the database. This query doesn't yield a {@link Result}.
 */
public class AddSmellResultQuery implements Query {

	/**
	 * The slime of interest.
	 */
	private final Slime slime;

	/**
	 * The smell to be added to its record.
	 */
	private final Smell targetSmell;

	/**
	 * Creates an add query.
	 * 
	 * @param slime
	 *            The slime of interest.
	 * @param targetSmell
	 *            The smell to be added to the record of the slime.
	 */
	public AddSmellResultQuery(Slime slime, Smell targetSmell) {
		this.slime = slime;
		this.targetSmell = targetSmell;
	}

	/**
	 * Evtl. ohne synchronized wegen referenz auf private felder
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see np13.sddb.queries.Query#applyToDB(java.util.Map)
	 */
	@Override
	public synchronized Result applyToDB(Map<Slime, List<Smell>> db) {
		if (db.get(slime) != null)
			db.get(slime).add(targetSmell);
		else {
			db.put(slime, new LinkedList<Smell>());
			db.get(slime).add(targetSmell);
		}
		return null;
	}

	/**
	 * Evtl. ohne synchronized wegen referenz auf private felder
	 */
	public synchronized Slime getSlime() {
		return slime;
	}

	/**
	 * Evtl. ohne synchronized wegen referenz auf private felder
	 */
	public synchronized Smell getTargetSmell() {
		return targetSmell;
	}

}
